package com.hjm.project.returnBlack.returnBlack.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.Utils;
import com.hjm.project.merchantInfo.merchantInfo.domain.MerchantInfo;
import com.hjm.project.merchantInfo.merchantInfo.mapper.MerchantInfoMapper;
import com.yxhd.encrypt.EncryptUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.returnBlack.returnBlack.mapper.ReturnBlackListMapper;
import com.hjm.project.returnBlack.returnBlack.domain.ReturnBlackList;
import com.hjm.project.returnBlack.returnBlack.service.IReturnBlackListService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 返现黑名单Service业务层处理
 * 
 * @author maker-platform
 * @date 2023-01-05
 */
@Service
public class ReturnBlackListServiceImpl implements IReturnBlackListService {
    @Resource
    private ReturnBlackListMapper returnBlackListMapper;
    @Resource
    private MerchantInfoMapper merchantInfoMapper;

    /**
     * 查询返现黑名单
     * 
     * @param id 返现黑名单ID
     * @return 返现黑名单
     */
    @Override
    public ReturnBlackList selectReturnBlackListById(Integer id) {
        return returnBlackListMapper.selectReturnBlackListById(id);
    }

    /**
     * 查询返现黑名单列表
     * 
     * @param returnBlackList 返现黑名单
     * @return 返现黑名单
     */
    @Override
    public List<ReturnBlackList> selectReturnBlackListList(ReturnBlackList returnBlackList) {
        return returnBlackListMapper.selectReturnBlackListList(returnBlackList);
    }

    /**
     * 查询返现黑名单列表
     *
     * @param returnBlackList 返现黑名单
     * @return 返现黑名单 一个结果
     */
    @Override
    public ReturnBlackList selectReturnBlackListOne(ReturnBlackList returnBlackList) {
        return returnBlackListMapper.selectReturnBlackListOne(returnBlackList);
    }

    /**
     * 新增返现黑名单
     * 
     * @param returnBlackList 返现黑名单
     * @return 结果
     */
    @Override
    public int insertReturnBlackList(ReturnBlackList returnBlackList, String addMan) {
        //通过身份证号获取黑名单，判断是否已存在
        ReturnBlackList returnBlack = returnBlackListMapper.selectReturnBlackListByIdNumber(returnBlackList.getIdNumber());
        if(returnBlack != null){
            return -99;
        }
        MerchantInfo merchantInfo = merchantInfoMapper.selectMerchantInfoById(returnBlackList.getMerno());
        returnBlackList.setAddDate(Utils.formateDate(2));
        returnBlackList.setAddMan(addMan);
        returnBlackList.setStatus(1);
        returnBlackList.setAgentName(merchantInfo.getAgentNo());
        returnBlackList.setIdNumberDec(merchantInfo.getLegalCerno());
        return returnBlackListMapper.insertReturnBlackList(returnBlackList);
    }

    /**
     * 修改返现黑名单
     * 
     * @param returnBlackList 返现黑名单
     * @return 结果
     */
    @Override
    public int updateReturnBlackList(ReturnBlackList returnBlackList) {
        returnBlackList.setDelDate(Utils.formateDate(2));
        returnBlackList.setStatus(2);
        return returnBlackListMapper.updateReturnBlackList(returnBlackList);
    }

    /**
     * 删除返现黑名单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteReturnBlackListByIds(String ids) {
        return returnBlackListMapper.deleteReturnBlackListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除返现黑名单信息
     * 
     * @param id 返现黑名单ID
     * @return 结果
     */
    @Override
    public int deleteReturnBlackListById(Integer id) {
        return returnBlackListMapper.deleteReturnBlackListById(id);
    }

    /**
     * 导入返现黑名单
     * @param dataList
     * @return
     */
    @Override
    public Map importReturnBlackList(List<ReturnBlackList> dataList, String addMan) throws Exception{
        Map result = new HashMap();
        result.put("msg","0000");
        result.put("code","导入成功！");
        for(ReturnBlackList black : dataList){
            //通过商户编号获取商户信息
            MerchantInfo merchantInfo = merchantInfoMapper.selectMerchantInfoById(black.getMerno());
            //验证商户是否存在
            if(merchantInfo == null){
                result.put("msg","0001");
                result.put("code","导入失败，失败原因：商户编号："+black.getMerno()+" 商户不存在！");
                return result;
            }
            //验证身份证号是否正确
            if(!EncryptUtils.getDecrypt(merchantInfo.getLegalCerno()).equals(black.getIdNumber())){
                result.put("msg","0001");
                result.put("code","导入失败，失败原因：商户编号："+black.getMerno()+" 与身份证号不匹配！");
                return result;
            }
            //验证商户名称是否正确
            if(!merchantInfo.getMerName().equals(black.getMerName())){
                result.put("msg","0001");
                result.put("code","导入失败，失败原因：商户编号："+black.getMerno()+" 与商户名称不匹配！");
                return result;
            }
            //通过身份证号获取黑名单，判断是否已存在
            ReturnBlackList returnBlackList = returnBlackListMapper.selectReturnBlackListByIdNumber(black.getIdNumber());
            if(returnBlackList != null){
                result.put("msg","0001");
                result.put("code","导入失败，失败原因：身份证号："+black.getIdNumber()+"已经存在！");
                return result;
            }
        }
        for(ReturnBlackList black : dataList){
            MerchantInfo merchantInfo = merchantInfoMapper.selectMerchantInfoById(black.getMerno());
            //拼装保存数据
            black.setBlackType(1);
            black.setStatus(1);
            black.setAddDate(Utils.formateDate(2));
            black.setAddMan(addMan);
            black.setAgentName(merchantInfo.getAgentNo());
            black.setIdNumberDec(merchantInfo.getLegalCerno());
            returnBlackListMapper.insertReturnBlackList(black);
        }
        return result;
    }
}
